﻿using YNTK.Dependency;
using Mapster;
using SqlSugar;
using System.Collections.Generic;
using YNTK.Common.Enum;
using System.Linq;

namespace YNTK.Common.Filter
{

    /// <summary>
    /// 通用分页结果返回
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [SuppressSniffer]
    public class TkPageResult<T> where T : new()
    {
        /// <summary>
        /// 分页
        /// </summary>
        public TkPageResult pagination { get; set; }

        /// <summary>
        /// 分页信息
        /// </summary>
        public PageDto page { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        public List<T> data { get; set; }

        /// <summary>
        /// 返回消息
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 成功或者失败
        /// </summary>
        public ResultTypeEnum resultType { get; set; }

        /// <summary>
        /// 替换sqlsugar分页
        /// </summary>
        /// <param name="page">分页泛型集合</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static dynamic SqlSugarPageResult(SqlSugarPagedList<T> page, string message = "")
        {
            return new
            {
                pagination = page.pagination.Adapt<TkPageResult>(),
                data = page.list,
                message = message,
                resultType= ResultTypeEnum.Success
            };
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="page">分页泛型集合</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static TkPageResult<T> Success(SqlSugarPagedList<T> page, string message = "")
        {
            return new TkPageResult<T>()
            {
                resultType = ResultTypeEnum.Success,
                data = page.list?.ToList(),
                page = page.pagination.Adapt<PageDto>(),
                message = message
            };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="page">分页泛型集合</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static TkPageResult<T> Fail(SqlSugarPagedList<T> page, string message = "")
        {
            return new TkPageResult<T>()
            {
                resultType = ResultTypeEnum.Fail,
                data = page.list?.ToList(),
                page = page.pagination.Adapt<PageDto>(),
                message = message
            };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="data">返回数据对象</param>
        /// <param name="pagination">分页</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static TkPageResult<T> Fail(ICollection<T> data, TkPageResult pagination, string message = "")
        {
            return new TkPageResult<T>()
            {
                resultType = ResultTypeEnum.Success,
                data = data?.ToList(),
                page = pagination.Adapt<PageDto>(),
                message = message
            };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static TkPageResult<T> Fail(string message = "")
        {
            return new TkPageResult<T>()
            {
                resultType = ResultTypeEnum.Fail,
                message = message,
                data=null
            };
        }
    }

    /// <summary>
    /// 通用分页结果返回
    /// </summary>
    [SuppressSniffer]
    public class TkPageResult : TkPageResult<object>
    {
        /// <summary>
        /// 页码
        /// </summary>
        public int pageIndex { get; set; }

        /// <summary>
        /// 页容量
        /// </summary>
        public int pageSize { get; set; }

        /// <summary>
        /// 总条数
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount { get; set; }
    }

    /// <summary>
    /// 分页信息
    /// </summary>
    public class PageDto
    {
        /// <summary>
        /// 页码
        /// </summary>
        public int pageIndex { get; set; }

        /// <summary>
        /// 页容量
        /// </summary>
        public int pageSize { get; set; }

        /// <summary>
        /// 总条数
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount { get; set; }
    }

}
